from core.basehandler import BaseHandler
from core.room import Room
from core.action import Action
from core.support import set_cutscene
#from core.support import get_game
import random

class Pool(Room):
    def __init__(self, room_id):
        Room.__init__(self, room_id)    # do basic initialization for every room
        
        # add inventory items
        #self.add_item(BrokenMeter())
        
        # build list of actions
        self.add_action(PlayWithUndergrad)
        self.add_action(PlayWithOldman)
        self.add_action(WalkAwayFromPool)
        #self.game = get_game() 
        #self.player = self.game.get_player()
        #self.player.pop_item('Pointer()')
        
    # return description of room.
    def get_description(self):
        return "You approach the pool tables. There are bunch of undergrads and a few drunk old men ready to play."
    
    # return image of room
    def get_image(self):
        return 'pool_image.jpg'


class PoolHandler(BaseHandler):
    
    # return class to use to create a new instance of this room
    def get_room_class(self):
        return Pool
    
    def get_template(self):
        player = self.game.get_player()
        if player.beers_count == 5:
            return 'room2.html'
        else:
            return 'room.html'
# define actions for this room

class PlayWithUndergrad(Action):
    def __init__(self):
        Action.__init__(self, "PlayWithUndergrad")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Play With Undergrad"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'pool'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "post"
    
    def do_action(self,game,room,request):
        p = game.get_player()
        # Make the probability of winning 80%. Implementing with random numbers. Update: Being drunk hurts your chances.
        n = random.randint(1,100)        
        if n <= 80 - p.beers_count*6: #winning
            p.confidence += 1
            set_cutscene("You beat the undergrad and gain confidence. Confidence level is "+str(p.confidence))
        else: #losing
            p.confidence -= 1
            set_cutscene("The undergrad beats you and you feel really ashamed. Confidence level is "+str(p.confidence))
        return

class PlayWithOldman(Action):
    def __init__(self):
        Action.__init__(self, "PlayWithOldman")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Play With Old Man"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'pool'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "post"
    
    def do_action(self,game,room,request):
        p = game.get_player()
        # Make the probability of winning 20%. Implementing with random numbers.
        n = random.randint(1,100)        
        if n <= 20: #winning
            p.confidence += 5
            set_cutscene("You beat the old man and gain a lot of confidence. Confidence level is "+str(p.confidence))
        else: #losing
            p.confidence -= 1
            set_cutscene("The undergrad beats you and you lose confidence. Confidence level is "+str(p.confidence))
        return
    
class WalkAwayFromPool(Action):
    def __init__(self):
        Action.__init__(self, "WalkAwayFromPool")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Walk Away From Pool"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'cornwalls'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"